﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using SmallERP.DAL;
using SmallERP.MODEL.FoundationModel;
using SmallERP.MODEL.PrintManage;
using Smallerp.Contracts;

namespace SmallERP.Bll.PrintManage
{
    public  class PaperWasteRecord
    {
        IOperateDB _OperateDb;
        public PaperWasteRecord(IOperateDB _DB)
        {
            _OperateDb = _DB;
        }
        public int Insert(PaperWasteRecordModel paperWasteRecordInfo, out int PaperWasteRecordId)
        {
            int ret = 0;
            //PaperWasteRecordId = 0;
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    try
            //    {
            //        PaperWasteRecords pWasteRecord = new PaperWasteRecords();
            //        pWasteRecord.CreateTime = paperWasteRecordInfo.CreateTime;
            //        pWasteRecord.Length = paperWasteRecordInfo.Length;
            //        pWasteRecord.PaperInfo = entities.PaperInfo.FirstOrDefault(p => p.PrintPaperId == paperWasteRecordInfo.PaperInfo.PrintPaperId);
            //        pWasteRecord.PaperWasteType = entities.PaperWasteType.FirstOrDefault(p => p.Id == paperWasteRecordInfo.PaperWasteType.Id);
            //        pWasteRecord.Remark = paperWasteRecordInfo.Remark;
            //        pWasteRecord.Status = paperWasteRecordInfo.Status;
            //        pWasteRecord.Width = paperWasteRecordInfo.Width;
            //        entities.AddToPaperWasteRecords(pWasteRecord);
            //        entities.SaveChanges();

            //        PaperWasteRecordId = pWasteRecord.Id;
            //    }
            //    catch
            //    {
            //        ret = -1;
            //    }
            //}
            
            string insertPaperWasteRecords = "insert into PaperWasteRecords(PaperWasteTypeId,PrintPaperId,Length,Width,CreateTime,Status,Remark) values("
                + paperWasteRecordInfo.PaperWasteType.Id + "," + paperWasteRecordInfo.PaperInfo.PrintPaperId + "," + paperWasteRecordInfo.Length + "," + paperWasteRecordInfo.Width + ",'" + paperWasteRecordInfo.CreateTime + "'," + paperWasteRecordInfo.Status + ",'" + paperWasteRecordInfo.Remark + "')";
            int inVal = _OperateDb.ExecuteNonQuery(insertPaperWasteRecords);
            if (inVal > 0)
            {
                PaperWasteRecordId = (int)_OperateDb.ExecuteScalar("select Id from PaperWasteRecords where createtime=#" + paperWasteRecordInfo.CreateTime + "#");
                ret = 0;
            }
            else
            {
                PaperWasteRecordId = 0;
                ret = -1;
            }
            return ret;
        }
    }
}
